package  
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author DarkGoodWIN
	 */
	public class TTextContainer extends TControl
	{
		private var LogRect: Trect = null;
		public var LogText: Ttext = null;
		private var Up: TControl = null;
		private var Down: TControl = null;
		
		public function Log(s:String): void
		{
			LogText.AddHTMLLine(s);
			var rect: Rectangle = LogText.text_field.scrollRect;
			rect.y = LogText.text_field.textHeight - rect.height + 5
			LogText.text_field.scrollRect = rect;
			CheckButtonsEnabled();
		}
		
		private function CreateButton(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int): TControl
		{
			var M: MovieClip = new MovieClip();
			M.graphics.beginFill(0x000000);
			
			M.graphics.moveTo(x1, y1);
			M.graphics.lineTo(x2, y2);
			M.graphics.lineTo(x3, y3);
			M.graphics.lineTo(x1, y1);
			
			M.graphics.endFill();
			
			return new TControl(M, LogRect, 0, 0);		
		}
		
		private function CheckButtonsEnabled(): void
		{
			var rect: Rectangle = LogText.text_field.scrollRect;
			if (rect.y <= 0)
			  Up.Ctrl.alpha = 0.3
			else
			  Up.Ctrl.alpha = 1;
			
			if (rect.y + rect.height >= LogText.text_field.textHeight)
			  Down.Ctrl.alpha = 0.3
			else
			  Down.Ctrl.alpha = 1;
		}
		
		private function OnUpClick(e: MouseEvent): void
		{
			var rect: Rectangle = LogText.text_field.scrollRect;
			if (rect.y <= 0)
			  return;
			rect.y -= 20;
			LogText.text_field.scrollRect = rect;			
			CheckButtonsEnabled();
		}
		
		private function OnDownClick(e: MouseEvent): void
		{
			var rect: Rectangle = LogText.text_field.scrollRect;
			if (rect.y + rect.height >= LogText.text_field.textHeight)
			  return;
			rect.y += 20;
			LogText.text_field.scrollRect = rect;
			CheckButtonsEnabled();
        }
		
		private function CreateButtons(): void
		{	  
			Up = CreateButton(0, 20, 10, 0, 20, 20);
			Up.X = Width - 20;
			Up.Y = 0;
			Up.addEventListener(MouseEvent.CLICK, OnUpClick);
			
			Down = CreateButton(0, 0, 10, 20, 20, 0);
			Down.X = Width - 20;
			Down.Y = LogText.text_field.scrollRect.height - 20;
			Down.addEventListener(MouseEvent.CLICK, OnDownClick);
		}
		
		public function TTextContainer(Parent: TControl,  Col: uint, x: int, y: int, aWidth: int, aHeight: int, Gradient: Boolean = false, 
	       RotateGradient: Boolean = false, DuplicateColor: Boolean = false, Col2: uint = 0xFFFFFF)
		{
    		super (new MovieClip(), Parent, x, y);
			LogRect = new Trect(this, Col, 0, 0, aWidth, aHeight, Gradient, RotateGradient, DuplicateColor, Col2);	
			LogText = new Ttext("", LogRect, 0, 0, false);
			LogText.text_field.width = aWidth - 20;
			LogText.text_field.height = 100000;
			LogText.text_field.multiline = true;
			LogText.text_field.wordWrap = true;
			LogText.text_field.cacheAsBitmap = true;
			LogText.text_field.scrollRect = new Rectangle(0, 0, aWidth - 20, aHeight);
			CreateButtons();
		}
		
	}

}